<?php
// $Id: pager.php 181 2012-02-27 05:32:22Z tomqin@gmail.com $

/**
 * Pager 分页管理
 *
 * @copyright		Copyright (C) 2010-2012 ARESCMS Inc.
 * @author			TomQin <tomqin@gmail.com>
 * @license			http://www.arescms.cn/license/
 * @package Core
 */

class Pager{

	/*分页显示参数设置*/
	public $page_size  = 0;  		//每页显示的记录数目
	public $link_num   = 5;   		//显示页码链接的数目
	public $page	   = 1;  		//页码
	public $records	= 0; 		//表中记录总数
	public $page_count = 0;  		//总页数
	public $pagestring = '';		//前後分页链接字符串

	public function __construct($records, $page_size, $link_num=10){
		$this->records   = intval($records);
		$this->page_size = intval($page_size);
		$this->link_num  = intval($link_num);

		$this->resetPage();
	}

	/**
	 * 获取数据偏移量
	 *
	 * @return integer
	 */
	public function getOffset(){
		return ($this->page - 1)*$this->page_size;
	}

	/**
	 * 翻页链接样式1（首页 上页 下页 尾页）
	 *
	 * @param string $link	翻页链接参数
	 * @return integer
	 */
	public function page_link1($link=null){
		//前後页链接字符串
		if($this->page==1){
			$this->pagestring.="首页 | 上一页";		 //如果是首页，无链接
		}else{
			$this->pagestring.="[<a href=?page=1$link>首页</a>] | [<a href=?page=".($this->page-1)."$link>上一页</a>]"; //不为首页，有链接
		}
		$this->pagestring.=" | ";
		if($this->page==$this->page_count||$this->page_count==0){
			$this->pagestring.="[下一页] | [尾页]";		   //如果是最後一页，无链接
		}else{
			$this->pagestring.="[<a href=?page=".($this->page+1)."$link>下一页</a>] | [<a href=?page=".$this->page_count."$link>尾页</a>]"; //不是最後一页，有链接
		}
		return $this->pagestring;
	}

	/**
	 * 翻页链接样式（ << 首 < 上一页 1 | 2 | 3 | 4 | 5 | 6 | 7 下一页 > 末 >>）
	 *
	 * @param string	翻页链接参数
	 * @param boolean
	 * @return string
	 */
	public function frontPageLink($link = '?page={PAGE}', $sample = false){
		$this->pagestring = '<p class="mod_pagenav_main"> ';

		//页码链接字符串
		if($this->page <= ceil($this->link_num / 2)) {
			$front_page = 1;
			$last_page  = ($this->page_count < $this->link_num) ? $this->page_count : $this->link_num;
			$t = 1;
		}elseif($this->page + ceil($this->link_num / 2)  > $this->page_count){
			$front_page = ($this->page_count < $this->link_num) ? 1 : $this->page_count - $this->link_num + 1;
			$last_page  = $this->page_count;
			$t = 2;
		} else {
			$front_page = $this->page - ceil($this->link_num / 2) + 1;
			$last_page  = $this->page + floor($this->link_num / 2);
			$t = 3;
		}

		$this->pagestring .= ($this->page <= 1)
						   ? '<span class="mod_pagenav_disable"><span>上一页</span></span>'
						   : '<a href="'.$this->link($link, $this->page - 1).'"><span>上一页</span></a>';
		$this->pagestring .= '&nbsp;';
		$this->pagestring .= '<span class="mod_pagenav_count">';

		$comma = ' ';
		for($i=$front_page; $i <= $last_page; $i++){
			$this->pagestring .= $comma;
			if($i == $this->page) $this->pagestring .= '<span class="current"><span>'.$i.'</span></span>';
			else $this->pagestring .= '<a href="'.$this->link($link, $i).'"><span>'.$i.'</span></a> ';
			$comma = '&nbsp;';
		}
		$this->pagestring .= '</span>&nbsp;';
		$this->pagestring .= ($this->page >= $this->page_count)
						   ? '<span class="mod_pagenav_disable"><span>下一页</span></span>'
						   : '<a href="'.$this->link($link, $this->page + 1).'"><span>下一页</span></a>';
		$this->pagestring .= '&nbsp;';
		$this->pagestring .= '</p>';

		if(!$sample){
			$this->pagestring .= '<p class="mod_pagenav_option">
				<span class="mod_pagenav_turn">到 <input id="jump_page" class="em">
				  页<button type="submit">Go</button></span> </p>';
		}
		return $this->pagestring;
	}

	/**
	 * 翻页链接样式（ << 首 < 上一页 1 | 2 | 3 | 4 | 5 | 6 | 7 下一页 > 末 >>）
	 *
	 * @param string	翻页链接参数
	 * @param boolean
	 * @return string
	 */
	public function adminPageLink($link = '?page={PAGE}', $sample = false){
		$this->pagestring = $sample ? '' :
			"<span>共 {$this->records} 条记录 / {$this->page_size} 页</span>";

		//页码链接字符串
		if($this->page <= ceil($this->link_num / 2)) {
			$front_page = 1;
			$last_page  = ($this->page_count < $this->link_num) ? $this->page_count : $this->link_num;
			$t = 1;
		}elseif($this->page + ceil($this->link_num / 2)  > $this->page_count){
			$front_page = ($this->page_count < $this->link_num) ? 1 : $this->page_count - $this->link_num + 1;
			$last_page  = $this->page_count;
			$t = 2;
		} else {
			$front_page = $this->page - ceil($this->link_num / 2) + 1;
			$last_page  = $this->page + floor($this->link_num / 2);
			$t = 3;
		}

		$this->pagestring .= '<a href="'.$this->link($link, 1).'">&lt;&lt;</a> <a href="'.$this->link($link, $this->page - 1).'">上一页</a> ';

		$comma = ' ';
		for($i=$front_page; $i <= $last_page; $i++){
			$this->pagestring .= $comma;
			if($i == $this->page) $this->pagestring .= '<strong class="current">'.$i.'</strong> ';
			else $this->pagestring .= '<a href="'.$this->link($link, $i).'">'.$i.'</a> ';
			$comma = ' ';
		}
		$this->pagestring .= ' <a href="'.$this->link($link, $this->page + 1).'">下一页</a> <a href="'.$this->link($link, $this->page_count).'">&gt;&gt;</a>';

		return $this->pagestring;
	}

	/**
	 * 页码处理
	 *
	 * @return void
	 */
	private function resetPage(){
		$this->page_count = ceil($this->records/$this->page_size);

		if(isset($_REQUEST['page']) && $_REQUEST['page']!=null){
			$this->page = intval($_REQUEST['page']);
			$this->page = ($this->page > $this->page_count) ? $this->page_count : (($this->page <= 0) ? 1 : $this->page);
		}else{
			$this->page = 1;
		}
	}

	/**
	 * 拼接分页链接
	 *
	 * @return integer
	 */
	private function link($link, $page) {
		return str_replace('{PAGE}', $page, $link);
	}
}
?>